package org.example.log.service;

import com.alibaba.fastjson.JSONObject;
import org.example.log.dto.SaveLogDto;
import org.example.log.service.threadPool.CustomThreadPool;
import org.example.log.task.SaveLoggingTask;
import org.example.log.tool.ModelFieldCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;


/**
 * 存储模型日志数据
 *
 * @author hero生仔
 * @date 2025/8/2
 */
@Service
public class SaveLoggingService {

    @Autowired
    private ModelFieldCache modelFieldCache;

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 存日志
     *
     * @param saveLogDto 日志数据DTO
     */
    public void saveLoggingTask(SaveLogDto saveLogDto) {
        String treeName = saveLogDto.getDcsTree();
        JSONObject modelFieldJsonObj = modelFieldCache.getModelFieldByTreeName(treeName);
        for (String modelName : modelFieldJsonObj.keySet()) {
            // 获取中间层映射字段
            JSONObject fieldJsonObject = modelFieldJsonObj.getJSONObject(modelName);
            SaveLoggingTask saveLoggingTask = applicationContext.getBean(SaveLoggingTask.class);
            saveLoggingTask.setBaseInfo(modelName, saveLogDto, fieldJsonObject);
            CustomThreadPool.execute(saveLoggingTask);
        }
    }
}
